<?php

class Application_Model_UnionUser_taobao extends Application_Model_UnionUser_Base {

    protected $client_id = '21170405';
    protected $callback = 'http://www.uuch.com/user/unioncallback/name/taobao';
    protected $client_secret = 'e7b2c2be477fcd7196e40c1f8b81a0ee';

    protected $userid;


    public function getLoginUrl() {
        $url = 'https://oauth.taobao.com/authorize?response_type=code&client_id=' . $this->client_id . '&redirect_uri=' . $this->callback;
        return $url;
    }

    public function getUnionName() {
        return '淘宝';
    }

    public function getUserKey() {
        $synKey = "taobaokey_" . $this->userid;
        return $synKey;
    }

    public function getUserInfo() {
        $result = new Application_Model_UnionUser_UserInfo();
        $result->nickname = $this->username;
        $result->userKey = "taobaokey_" . $this->userid;
        return $result;
    }

    public function isValid() {
        $code = $_REQUEST['code'];   //通过访问https://oauth.taobao.com/authorize获取code
        $grant_type = 'authorization_code';

        //请求参数
        $postfields = array('grant_type' => $grant_type,
            'client_id' => $this->client_id,
            'client_secret' => $this->client_secret,
            'code' => $code,
            'redirect_uri' => $this->callback
        );

        $url = 'https://oauth.taobao.com/token';

        $token = json_decode($this->taobao_curl($url, $postfields));
        //$access_token = $token->access_token;

        //打印token
        $userid = $token->taobao_user_id;
        $username = urldecode($token->taobao_user_nick);
        $this->userid = $userid;
        $this->username = $username;
        return $userid;
    }
    
    protected $username;

    //POST请求函数
    protected function taobao_curl($url, $postFields = null) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        if (is_array($postFields) && 0 < count($postFields)) {
            $postBodyString = "";
            foreach ($postFields as $k => $v) {
                $postBodyString .= "$k=" . urlencode($v) . "&";
            }
            unset($k, $v);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1));
        }
        $reponse = curl_exec($ch);
        if (curl_errno($ch)) {
            throw new Exception(curl_error($ch), 0);
        } else {
            $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            
            if (400 === $httpStatusCode) {
                return $httpStatusCode;
            }
            
            if (200 !== $httpStatusCode) {
                throw new Exception($reponse, $httpStatusCode);
            }
            
            
            
        }
        curl_close($ch);
        return $reponse;
    }

}